package my.common.util.http.bean;

import my.common.util.string.StringUtil;
import my.common.util.http.HttpUtils;


public class RemoteRestfulAPIBean {

	private RequestBean requestBean;

	public RequestBean getRequestBean() {
		return requestBean;
	}

	public void setRequestBean(RequestBean requestBean) {
		this.requestBean = requestBean;
	}

	public ResponseBean invoke() throws Exception {
		if (this.requestBean == null) {
			throw new Exception("请求体错误!");
		}
		if (!StringUtil.allIsNotEmpty(requestBean.getMethod(),
				requestBean.getUrl())) {
			throw new Exception("路径和方法必须不为空！");
		}
		ResponseBean respBean = null;
		switch (requestBean.getMethod().toLowerCase()) {
		case "get":
			respBean = HttpUtils.sendGet(requestBean.getUrl(),
					requestBean.getRequestHeaders(),
					requestBean.getRequestBody());
			break;
		case "post":
			respBean = HttpUtils.sendPost(requestBean.getUrl(),
					requestBean.getRequestHeaders(),
					requestBean.getRequestBody());
			break;
		case "put":
			respBean = HttpUtils.sendPut(requestBean.getUrl(),
					requestBean.getRequestHeaders(),
					requestBean.getRequestBody());
			break;
		}
		return respBean;
	}

}
